package com.henzil.siji;

import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.henzil.siji.bean.VehiclesInfo;
import com.henzil.siji.data.DriverContentProvider;

public class AddCarActivity extends Activity{

	private EditText carLicenseEditText;
	
	private EditText engineModelEditText;
	
	private VehiclesInfo info;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.addcar);
		setVehiclesInfo();
		initView();
		
	}
	private void setVehiclesInfo(){
		Bundle bundle = getIntent().getExtras();
		int vehiclesInfo_id = -1;
		if(bundle != null){
			vehiclesInfo_id = bundle.getInt("vehiclesInfo_id");
		}
		if(vehiclesInfo_id > -1){
			Cursor cursor = managedQuery(DriverContentProvider.CONTENT_URI, null,
					null, null, null);
			
			while (cursor.moveToNext()) {
				info = new VehiclesInfo();
				info.setId(cursor.getInt(0));
				info.setCarLicense(cursor.getString(1));
				info.setEngineModel(cursor.getString(2));
			}
			cursor.close();
		} else {
			info = null;
		}
	}
	private void initView(){
		
		carLicenseEditText = (EditText) findViewById(R.id.chepaihao);
		engineModelEditText = (EditText) findViewById(R.id.fadongjihao);
		
		findViewById(R.id.ok_button).setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if(addVehiclesInfo()){
					setResult(RESULT_OK);
					finish();
				}
			}
		});
		
		findViewById(R.id.cancel_button).setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				setResult(RESULT_CANCELED);
				finish();
			}
		});
	}
	//判断是否添加VEHICLESINFO成功
	private boolean addVehiclesInfo(){
		String carLicenseStr = carLicenseEditText.getText().toString();
		String engineModelStr = engineModelEditText.getText().toString();
		
		if(carLicenseStr.length() < 6){
			Toast.makeText(AddCarActivity.this, "车牌号:太短了，长度应该不少于6位",
                    Toast.LENGTH_SHORT).show();
		} else if(carLicenseStr.length() > 7){
			Toast.makeText(AddCarActivity.this, "车牌号:太长了，长度应该不大于7位",
                    Toast.LENGTH_SHORT).show();
		} else {
			if(engineModelStr.length()<=0){
				Toast.makeText(AddCarActivity.this, "请输入发动机号",
	                    Toast.LENGTH_SHORT).show();
			} else {
				ContentValues contentValues = new ContentValues();
				contentValues.put(DriverContentProvider.CARLICWNSE, carLicenseStr);
				contentValues.put(DriverContentProvider.ENGINEMODEL, engineModelStr);
				if(info != null){
					String[] strs = {String.valueOf(info.getId())};
					getContentResolver().update
					(DriverContentProvider.CONTENT_URI_UPDATE_VEHICLESINFO, contentValues, DriverContentProvider._ID+"=?", strs);
				} else {
					getContentResolver().insert(DriverContentProvider.CONTENT_URI_ADD_VEHICLESINFO, contentValues);
				}
				
				return true;
			}
		}
		return false;
	}
}
